<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;

function createReasonableTable($tableName) {
    if(Schema::hasTable($tableName)) {
        return ;
    }
    Schema::create($tableName, function (Blueprint $table) {
        $table->integer('id')->autoIncrement();
        $table->string('error_code', 30)->nullable()->comment('错误码');
        $table->text('error_message')->nullable()->comment('错误消息');
        $table->string('error_file_path', 255)->nullable()->comment('文件路径');
        $table->integer('error_file_line')->nullable()->comment('行号');
        $table->longtext('error_trace')->nullable()->comment('错误trace');
        $table->datetime('created_at')->nullable()->comment('创建时间');
        $table->datetime('updated_at')->nullable()->comment('更新时间');
    });
}

function createOperateLogTable($tableName) {
    if(Schema::hasTable($tableName)) {
        return ;
    }
    Schema::create($tableName, function (Blueprint $table) {
        $table->integer('id')->autoIncrement();
        $table->string('method', 30)->nullable()->comment('请求方法');
        $table->string('path', 255)->nullable()->comment('请求路径');
        $table->text('request_data')->nullable()->comment('请求数据');
        $table->text('response_data')->nullable()->comment('响应数据');
        $table->string('client_ip', 30)->nullable()->comment('客户IP');
        $table->datetime('created_at')->nullable()->comment('创建时间');
        $table->datetime('updated_at')->nullable()->comment('更新时间');
    });
}

function isDebug() {
    return config('app.debug') === true;
}

function toJson(array $arr)
{
    return json_encode($arr);
}

function fromJson(string $json, $collect = false)
{
    $arr = json_decode($json, true);
    if($collect) {
        return collect($arr);
    }
    return $arr;
}

function getToken()
{
    return request()->bearerToken();
}

/**
 * 环境判断
 *
 * @param [type] $env
 * @return boolean
 */
function isEnv($env)
{
    return config('app.env') == $env;
    return app()->environment() === $env;
}

/**
 * 日志记录
 *
 * @param string $content
 */
function writeLog()
{
    $args = func_get_args();
    foreach($args as $arg) {
        $content = "";
        if(is_array($arg) || is_object($arg)) {
            $content = json_encode($arg);
        } else {
            $content = $arg;
        }

        Log::info(sprintf("%s \n", $content));
    }
}

/**
 * 获取分页
 *
 * @param Request $request
 * @return void
 */
function getPage(Request $request)
{
    $page = intval($request->page);
    if($page < 1) {
        $page = 1;
    }

    $psize = intval($request->page_size);
    if($psize < 1) {
        $psize = PAGE_SIZE;
    }
    $skip = ($page - 1) * $psize;
    return [$page, $psize, $skip];
}


// 应用公共文件
function success($data=[], $msg='ok', $code=200)
{
    $info = [
        'success' => true,
        'code'    => $code,
        'message' => $msg,
        'data'    => $data
    ];
    if(is_null($data)){
        unset($info['data']);
    }
    return response()->json($info)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
}

function pageSuccess($count=0, $data=[], $msg="ok", $code=200)
{
    return response()->json([
        'success' => true,
        'code'  => $code,
        'message'=> $msg,
        'count' => $count,
        'data'  => $data
    ])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
}

function error($msg='', $code=-1, $trace=[])
{
    $errorData = [
        'success' => false,
        'code' => $code,
        'message' => $msg,
    ];
    if($trace) {
        $errorData['trace'] = $trace;
    }
    return response()->json($errorData)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
}

function getUserId() {
    return auth()->user()->id;
}
